package com.cheer.ba.view.activity;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;

import com.cheer.ba.R;
import com.cheer.ba.cache.preference.Session;
import com.cheer.ba.constant.ConstantTag;
import com.cheer.ba.constant.Constants;
import com.cheer.ba.constant.IntentCode;
import com.cheer.ba.constant.IntentKey;
import com.cheer.ba.model.ConstantModel;
import com.cheer.ba.model.OilSendModel;
import com.cheer.ba.model.UserInfoModel;
import com.cheer.ba.presenter.impl.TransitPresenter;
import com.cheer.ba.utils.Currency;
import com.cheer.ba.utils.StringUtils;
import com.cheer.ba.view.base.BaseCommAty;

/**
 * 过度界面
 */
public class TransitAty extends BaseCommAty {

    //套餐id
    private String productId;
    //油卡id
    private String oilCardId;
    //支付原价
    private String amount;
    //支付金额
    private String money;
    //手续费
    private String feeAmount;
    //卡号
    private String mCardNum;
    //从哪个界面跳转
    private int page_from;

    private String mSaveKey;

    //记录从柴油批发订单界面传过来的数据
    private OilSendModel mOilSendModel;

    /**
     * 激活油卡
     *
     * @param activity
     * @param page      哪个界面跳转
     * @param oilCardId 油卡id
     * @param feeAmount 手续费
     * @param mCardNum  油卡卡号
     */
    public static void startTransitAty(Activity activity, int page, String oilCardId, String feeAmount, String mCardNum) {
        Intent intent = new Intent(activity, TransitAty.class);
        intent.putExtra(IntentKey.General.KEY_PAGE, page);
        intent.putExtra(IntentKey.Oil.KEY_OIL_CARD_ID, oilCardId);
        intent.putExtra(IntentKey.General.KEY_PAY_FEEAMOUNT, feeAmount);
        intent.putExtra(IntentKey.General.KEY_CARD_NUM, mCardNum);
        activity.startActivity(intent);
    }

    /**
     * 获取我的个人信息
     *
     * @param activity
     * @param page     哪个界面跳转
     */
    public static void start2TransitAty(Activity activity, int page) {
        Intent intent = new Intent(activity, TransitAty.class);
        intent.putExtra(IntentKey.General.KEY_PAGE, page);
        activity.startActivity(intent);
    }

    /**
     * 充值界面跳转
     *
     * @param activity
     * @param productId 套餐id
     * @param oilCardId 油卡id
     * @param amount    支付原价
     * @param feeAmount 手续费
     */
    public static void start3TransitAty(Activity activity, int page, String productId, String oilCardId, String amount, String mpayMoney, String feeAmount, String mCardNum) {
        Intent intent = new Intent(activity, PayAty.class);
        intent.putExtra(IntentKey.General.KEY_PAGE, page);
        intent.putExtra(IntentKey.Oil.KEY_OIL_PRODUCTID, productId);
        intent.putExtra(IntentKey.Oil.KEY_OIL_CARD_ID, oilCardId);
        intent.putExtra(IntentKey.General.KEY_PAY_AMOUNT, amount);
        intent.putExtra(IntentKey.General.KEY_PAY_MONEY, mpayMoney);
        intent.putExtra(IntentKey.General.KEY_PAY_FEEAMOUNT, feeAmount);
        intent.putExtra(IntentKey.General.KEY_CARD_NUM, mCardNum);
        activity.startActivity(intent);
    }

    /**
     * 柴油批发下单,获取个人信息
     *
     * @param activity
     * @param page     哪个界面跳转
     */
    public static void start4TransitAty(Activity activity, int page, OilSendModel mOilSendModel) {
        Intent intent = new Intent(activity, TransitAty.class);
        intent.putExtra(IntentKey.General.KEY_PAGE, page);
        intent.putExtra(IntentKey.General.KEY_MODEL, mOilSendModel);
        activity.startActivity(intent);
    }

    @Override
    public View loadView(LayoutInflater inflater, View childView) {
        return super.loadView(inflater, inflater.inflate(R.layout.aty_transit, null, false));
    }

    @Override
    public void initPresenter() {
        presenter = new TransitPresenter();
    }

    @Override
    public void initView() {
        page_from = getIntent().getIntExtra(IntentKey.General.KEY_PAGE, Constants.DEFAULTVALUE);
        if (page_from == IntentCode.General.JUMP_CARD_DETAIL) {
            oilCardId = getIntent().getStringExtra(IntentKey.Oil.KEY_OIL_CARD_ID);
            feeAmount = getIntent().getStringExtra(IntentKey.General.KEY_PAY_FEEAMOUNT);
            mCardNum = getIntent().getStringExtra(IntentKey.General.KEY_CARD_NUM);

            //去激活
            ((TransitPresenter) presenter).getSystemConstant("activation_value", ConstantTag.GET_CONSTANT_SUCCESS);
        } else if (page_from == IntentCode.General.JUMP_MY_INFO || page_from == IntentCode.General.JUMP_HOME_RECHARGE) {
            //获取个人信息
            ((TransitPresenter) presenter).getUserInfo();
        } else if (page_from == IntentCode.General.JUMP_RECHARGE) {
            //充值跳转
            productId = getIntent().getStringExtra(IntentKey.Oil.KEY_OIL_PRODUCTID);
            oilCardId = getIntent().getStringExtra(IntentKey.Oil.KEY_OIL_CARD_ID);
            amount = getIntent().getStringExtra(IntentKey.General.KEY_PAY_AMOUNT);
            money = getIntent().getStringExtra(IntentKey.General.KEY_PAY_MONEY);
            feeAmount = getIntent().getStringExtra(IntentKey.General.KEY_PAY_FEEAMOUNT);
            mCardNum = getIntent().getStringExtra(IntentKey.General.KEY_CARD_NUM);

            ((TransitPresenter) presenter).getUserInfo();
        } else if (page_from == IntentCode.General.JUMP_CARD_ORDER) {
            //柴油批发
            mOilSendModel = (OilSendModel) getIntent().getSerializableExtra(IntentKey.General.KEY_MODEL);

            ((TransitPresenter) presenter).getUserInfo();
        }
    }

    @Override
    public <T> void updateViewWithTag(T t, String tag) {
        if (null != t) {
            if (TextUtils.equals(tag, ConstantTag.GET_CONSTANT_SUCCESS)) {
                //去激活
                ConstantModel mConstantModel = (ConstantModel) t;
                mSaveKey = mConstantModel.key;
                if (StringUtils.isEmpty(mSaveKey)) {
                    dataStatus(2);
                } else {
                    ((TransitPresenter) presenter).getUserInfo();
                }
            } else if (TextUtils.equals(tag, ConstantTag.MY_AUTH_QYE)) {
                //企业认证成功
                UserInfoModel userModel = (UserInfoModel) t;
                if (null != userModel) {
                    Session.setUserOrgType(userModel.getOrgType());
                    if (StringUtils.isEmpty(userModel.getOrgId()) || TextUtils.equals("0", userModel.getOrgId())) {
                        Session.setUserOrgId("");
                    } else {
                        Session.setUserOrgId(userModel.getOrgId());
                    }
                }
                if (page_from == IntentCode.General.JUMP_HOME_RECHARGE) {
                    //从首页进入
                    skipActivity(aty, OilSetMealAty.class);
                } else if (page_from == IntentCode.General.JUMP_MY_INFO) {
                    //从我的界面进入
                    if (StringUtils.isEmpty(Session.getUserOrgId())) {
                        skipActivity(aty, QyeAuthListAty.class);
                    } else {
                        skipActivity(aty, QyeAuthAty.class);
                    }
                } else if (page_from == IntentCode.General.JUMP_RECHARGE) {
                    //充值跳转
                    PayAty.startPayAty(aty, page_from, productId, oilCardId, amount, money, "0", mCardNum);
                    finish();
                } else if (page_from == IntentCode.General.JUMP_CARD_DETAIL) {
                    //去激活
                    if (StringUtils.isEmpty(mSaveKey)) {
                        dataStatus(2);
                    } else {
                        //去激活
                        PayAty.start2PayAty(aty, IntentCode.General.JUMP_CARD_DETAIL, oilCardId, mSaveKey, Currency.returnDollar(mSaveKey).replace("元", ""), feeAmount, mCardNum);
                        finish();
                    }
                } else if (page_from == IntentCode.General.JUMP_CARD_ORDER) {
                    //从柴油批发界面进入
                    PayAty.start3PayAty(aty, page_from, mOilSendModel);
                    finish();
                }
            }
        }
    }
}
